<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" customClass="customWidth" destroy-on-close @closed="$emit('closed')">
		<el-form :model="form" :rules="rules" ref="dialogForm" label-width="100px" label-position="left">
			<el-form-item label="信息标题" prop="title">
				<el-input v-model="form.title" placeholder="信息标题" maxlength="80" clearable></el-input>
			</el-form-item>
			<el-row :gutter="24">
				<el-col :lg="12">
					<el-form-item label="信息类型" prop="dataType">
						<sc-select v-model="form.dataType" :params="{code:'updata'}" :apiObj="$API.system.sysDic.getSysDicByCode" clearable filterable style="width: 100%"></sc-select>
					</el-form-item>
				</el-col>
				<el-col :lg="12">
					<el-form-item label="处理方式" prop="approvedType">
						<sc-select v-model="form.approvedType" :params="{code:'approvedType'}" :apiObj="$API.system.sysDic.getSysDicByCode" clearable filterable style="width: 100%"></sc-select>
					</el-form-item>
				</el-col>
			</el-row>
			<el-row :gutter="24">
				<el-col :lg="12">
					<el-form-item label="上报部门" prop="deptId">
						<select-dept-dialog v-model:deptId="form.deptId" :mode="false"/>
					</el-form-item>
				</el-col>
				<el-col :lg="12">
					<el-form-item label="上报人" prop="fromAccountId">
						<select-user-dialog v-model:accountId="form.fromAccountId" :mode="false"/>
					</el-form-item>
				</el-col>
			</el-row>
			<el-row :gutter="24">
				<el-col :lg="12">
					<el-form-item label="信息处理人" prop="approvedUser">
						<select-user-dialog v-model:accountId="form.approvedUser" :mode="false"/>
					</el-form-item>
				</el-col>
				<el-col :lg="12">
					<el-form-item label="上报时间" prop="sendTime">
						<el-date-picker v-model="form.sendTime" placeholder="上报时间" type="date" value-format="YYYY-MM-DD" style="width: 100%;"></el-date-picker>
					</el-form-item>
				</el-col>
			</el-row>
			<el-form-item label="传阅对象" prop="toUser">
				<select-user-dialog v-model:accountId="form.toUser" :mode="true"/>
			</el-form-item>
			<el-form-item label="信息内容">
				<sc-editor v-model="form.content" placeholder="请输入" :height="400" clearable style="width: 100%;"></sc-editor>
			</el-form-item>
			<el-form-item label="相关附件" prop="attachId">
				<sc-upload-file :apiObj="apiObj" v-model="fileList" :data="{module:'datainfoattach'}" :showFileList="false" @success="addNewFile">
					<el-button type="primary" icon="el-icon-upload">上传附件</el-button>
				</sc-upload-file>
				<attach-menu v-model:attachIds="form.attachId" :roleFlag="4" :preview="true"></attach-menu>
			</el-form-item>
			<msg-box-check-box v-model:msgType="form.msgType" code="upload-data"></msg-box-check-box>
		</el-form>
		<template #footer>
			<el-button @click="visible=false">取 消</el-button>
			<el-button v-if="mode!='show'" type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
		</template>
	</el-dialog>

</template>
<script>
import selectUserDialog from "@/views/module/selectUserDialog";
import selectDeptDialog from "@/views/module/selectDeptDialog";
import {defineAsyncComponent} from "vue";
import msgBoxCheckBox from "@/views/module/msgBoxCheckBox";
import AttachMenu from '@/views/module/attachMenu'
const scEditor = defineAsyncComponent(() => import('@/components/scEditor'));
export default {
	emits: ['success', 'closed'],
	components: {
		msgBoxCheckBox,
		selectUserDialog,
		selectDeptDialog,
		AttachMenu,
		scEditor
	},
	data() {
		return {
			visible: false,
			isSaveing: false,
			isShowMsg: false,
			mode: "add",
			titleMap: {
				add: '信息上报对话框',
				edit: '信息编辑对话框',
			},
			fileList: [],
			apiObj: this.$API.file.attach.uploadFile,
			form: {
				recordId: '',
				title: '',
				dataType: '',
				approvedType: '',
				deptId: '',
				fromAccountId: this.$TOOL.data.get("USER_INFO").accountId,
				approvedUser: '',
				sendTime: '',
				toUser: '',
				content: '',
				attachId: '',
				msgType: ''
			},
			rules: {
				title: [
					{required: true, message: '信息标题不能为空', trigger: 'blur'}
				],
				fromAccountId: [
					{required: true, message: '上报人不能为空'}
				],
				deptId: [
					{required: true, message: '上报部门不能为空'}
				],
				dataType: [
					{required: true, message: '信息类型不能为空'}
				],
				approvedType: [
					{required: true, message: '处理方式不能为空'}
				],
				sendTime: [
					{required: true, message: '上报时间不能为空',trigger: 'blur'}
				]
			}
		}
	},
	mounted() {
	},
	methods: {
		//显示
		open(mode = 'add') {
			this.mode = mode;
			this.visible = true;
			return this
		},
		submit() {
			this.$refs.dialogForm.validate(async (valid) => {
				if (valid) {
					this.isSaveing = true;
					var res;
					if (this.mode == "add") {
						res = await this.$API.info.uploadData.insertDataUploadInfo.post(this.form);
					} else if (this.mode == "edit") {
						res = await this.$API.info.uploadData.updateDataUploadInfo.post(this.form);
					}
					this.isSaveing = false;
					if (res.code == 200) {
						this.$emit('success')
						this.visible = false;
						this.$message.success("操作成功")
					} else {
						this.$alert(res.message, "提示", {type: 'error'})
					}
				}
			})
		},
		setData(data) {
			this.form.recordId = data.recordId
			this.form.title = data.title
			this.form.dataType = data.dataType
			this.form.approvedType = data.approvedType
			this.form.deptId = data.deptId
			this.form.fromAccountId = data.fromAccountId
			this.form.approvedUser = data.approvedUser
			this.form.sendTime = data.sendTime
			this.form.toUser = data.toUser
			this.form.content = data.content
			this.form.attachId = data.attachId
			this.form.msgType = data.msgType
		},
		addNewFile(res, file)
		{
			let tArr =[];
			if(this.form.attachId!=undefined&&this.form.attachId!=null&&this.form.attachId!=''){
				tArr = this.form.attachId.split(",");
			}
			tArr.push(res.data.attachId);
			this.form.attachId = tArr.join(",");
		}
	}
}
</script>

<style>
.customWidth {
	width: 60%;
}
</style>
